package cn.tedu.store.controller;
/**
 * 控制器的基类
 * @author DELL
 *
 */

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ExceptionHandler;

import cn.tedu.store.controller.ex.FileSizeException;
import cn.tedu.store.controller.ex.FileTypeException;
import cn.tedu.store.controller.ex.FileUploadEmptyException;
import cn.tedu.store.controller.ex.FileUploadException;
import cn.tedu.store.controller.ex.FileUploadIOException;
import cn.tedu.store.controller.ex.FileUploadStateException;
import cn.tedu.store.service.ex.AccessDeniedException;
import cn.tedu.store.service.ex.AddressNotFoundException;
import cn.tedu.store.service.ex.AddressSizeLimitException;
import cn.tedu.store.service.ex.DeleteException;
import cn.tedu.store.service.ex.InsertException;
import cn.tedu.store.service.ex.PasswordNotMatchException;
import cn.tedu.store.service.ex.ProductNotFoundException;
import cn.tedu.store.service.ex.ServiceException;
import cn.tedu.store.service.ex.UpdateException;
import cn.tedu.store.service.ex.UserNotFoundException;
import cn.tedu.store.service.ex.UsernameConflictException;
import cn.tedu.store.util.JsonResult;

public class BaseController {
	/*
	 * 响应成功的标识码
	 */
	public static final int SUCCESS = 2000;

	protected Integer getUidFromSession(HttpSession session) {

		return Integer.valueOf(session.getAttribute("uid").toString());

	}
	
	protected String getUsernameFromSession(HttpSession session) {

		return session.getAttribute("username").toString();

	}

	@ExceptionHandler({ServiceException.class,FileUploadException.class})
	public JsonResult<Void> handlerException(Throwable e) {

		JsonResult<Void> Js = new JsonResult<>(e);

		if (e instanceof UsernameConflictException) {
			// 4000 - 用户名冲突异常，例如：注册时用户名已经注册
			Js.setState(4000);
		} else if (e instanceof UserNotFoundException) {
			// 4001-用户数据不存在
			Js.setState(4001);
		} else if (e instanceof PasswordNotMatchException) {
			// 4002-验证密码失败
			Js.setState(4002);
		} else if (e instanceof AddressSizeLimitException) {
			// 4003-收货地址上限异常
			Js.setState(4003);
		} else if (e instanceof AddressNotFoundException) {
			// 4004-收货地址不存在异常
			Js.setState(4004);
		} else if (e instanceof AccessDeniedException) {
			// 4005-归属问题异常
			Js.setState(4005);
		} else if (e instanceof ProductNotFoundException) {
			// 4006-商品信息不存在异常
			Js.setState(4006);
		} else if (e instanceof InsertException) {
			// 5000-插入数据异常
			Js.setState(5000);
		} else if (e instanceof UpdateException) {
			// 5001-修改数据异常
			Js.setState(5001);
		} else if (e instanceof DeleteException) {
			// 5002-删除数据异常
			Js.setState(5002);
		} else if (e instanceof FileUploadEmptyException) {
			// 6000-文件上传为空的异常
			Js.setState(6000);
		} else if (e instanceof FileSizeException) {
			// 6001-文件上传过大的异常
			Js.setState(6001);
		} else if (e instanceof FileTypeException) {
			// 6002-文件上传类型的异常
			Js.setState(6002);
		} else if (e instanceof FileUploadStateException) {
			// 6003-文件上传类型的异常
			Js.setState(6003);
		} else if (e instanceof FileUploadIOException) {
			// 6004-文件上传类型的异常
			Js.setState(6004);
		}

		return Js;
	}
}
